here is the problem in more detail...
It is the same as with "super(self.__class__, self).meth()", that is, as soon as you change, or combine metaclasses for some child class, you'll get wrong method resolution, and most likely fall into infinite recursion... (see example below).
the simplest solution would be to pass the metaclass to super explicitly, but this is even worse than passing the class explicitly in the general case, as the class and the metaclass can be located in different modules/packages... etc. I can't say that this will contribute to code clarity :)
example:
metaclass_example_killer.py
P.S. I made a little typo in the module name in the main post (the word example was lacking the "p" :) ), now this is corrected...
this was originaly posted here.
the simplest solution would be to pass the metaclass to super explicitly, but this is even worse than passing the class explicitly in the general case, as the class and the metaclass can be located in different modules/packages... etc. I can't say that this will contribute to code clarity :)
example:
import metaclass_example
class _OtherMetaClass(type):
'''
'''
def meth(cls):
print 'this is the other metaclass method!!'
# here super is in a try block so as to make the method usable
# as is (not only from class combinations).
# ....a terminated super! :))
try:
super(cls.__class__, cls).meth()
except AttributeError:
pass
class SomeUsefullClass(object):
'''
'''
__metaclass__ = _OtherMetaClass
# NOTE: here we inherit from two classes which are of different types
# or metaclasses, thus creating a metaclass conflict.
# to resolve this conflict we need to "combine" the metaclasses
# of the parent classes....
class _CombinationMetaclass(_OtherMetaClass, metaclass_example._MetaClass):
'''
'''
pass
# and here is an innocent looking result.... :)
class KillerClass(metaclass_example.SomeOtherClass, SomeUsefullClass):
'''
'''
__metaclass__ = _CombinationMetaclass
def meth(cls):
'''
'''
print 'hi from the killer :)'
super(cls.__class__, cls).meth()
if __name__ == '__main__':
SomeUsefullClass.meth()
killer = KillerClass()
print 'BANG!!!'
killer.meth()
# vim: set ts=4 sw=4 nowrap :
metaclass_example_killer.py
P.S. I made a little typo in the module name in the main post (the word example was lacking the "p" :) ), now this is corrected...
this was originaly posted here.